<?php
	/**
 	 * Main controller class
 	 */
	class MainController extends Controller
	{
		/**
		 * system logging usage
		 */
		public $use_logging = false;
		
		/**
	 	 * page title
	 	 */
		public $page_title = LANG_WELCOME;
		
		/**
	 	 * controller name
	 	 */
		public $name = 'main';

		/**
	 	 * menu item name
	 	 */
		public $menuItem = 'main';
		
		/**
	 	 * page layout
	 	 */
		public $layout = 'main';
		
		/**
	 	 * current module
	 	 */
		public $module = 'helpdesk';
		
		/**
	 	 * necessary helpers
	 	 */
		public $helpers = array ('html', 'htmlx', 'grids', 'blanks');
		
		/**
	 	 * necessary components
	 	 */
		public $components = array ('');
		
		/**
		 * Defines whether to check access (default: true)
		 *
		 * @var boolean
		 */
		public $checkAccess = false;

		/**
		 * defines whether the controller is to be cached
		 *
		 * @var boolean
		 */
		public $isCached = false;
		
		/**
	 	 * displays welcome page
	 	 */
		function index()
		{
			//business logic here
		}
		
		/**
	 	 * checks user login info
	 	 */
		function login()
		{
			global $object;
			
			$this->layout = 'empty';
			
			//inserts into the DB users, who unsuccessfully tried to log in more than 2 times
			if (!isset($_SESSION[suspect]))
		 	{
				$_SESSION[suspect] = 0;	
			}
			else
			{
				$_SESSION[suspect] = $_SESSION[suspect] + 1;
			}
			if ($_SESSION[suspect] == 3)
			{			
//				$suspects_model = $this->GetModelObject('suspects');
//				$suspects_model->Update(array('IP'=>$this->GetIp()));
			}
			
			//analyzes post array
			if ($this->request->post)
			{
				$users_model = $this->GetModelObject('users');
				$password = md5($this->request->post[password]);
				$check = $this->GetModelObject('Users')->Execute(array('login'=>$this->request->post[login], 'password'=>$password), 'sp_users_authentication');
				
				//if authorization failed
				if (empty($check))
				{
					$this->data[login] = $this->request->post[login];
					$this->data[error] = LANG_LOGIN_FAILED;
				}
				
				else
				{	
					//unsets suspect counter, set SESSION variables
					unset($_SESSION[suspect]);
					$_SESSION[userlogin] = $check[login];
					$_SESSION[userid] = $check[id];
					$_SESSION[usertheme] = $check[theme];
					$_SESSION[userresultsperpage] = $check[results_per_page];
					$_SESSION[lang] = $check[language_system_name];
					$_SESSION[dragndrop] = unserialize($check[dragndrop]);

					//gets system_settings for the logged user and registers them in the session
					$system_settings = $this->GetModelObject('SystemSettings')->ListItems();
					foreach ($system_settings as $setting)
					{
						$_SESSION[system_settings][$setting[name]] = $setting[value];
					}					
					
					if ($object->config->use_table_access)
					{
						//gets table access for the logged user and registers it in the session
						$table_accesses = $this->GetModelObject('SystemAccess')->ListItems(array('user_id'=>$_SESSION[userid]), 'sp_system_access_check');
						foreach ($table_accesses as $key=>$value)
						{
							$_SESSION[table_accesses][$value[table_name]] = $value;
						}
						
						//gets module access for the logged user and registers it in the session
						foreach (Menus::$contextual as $key=>$value)
						{
							foreach ($value[sub] as $sub)
							{
								if (!empty($_SESSION[table_accesses][$sub[table_name]][access_list]))
								{
									$modules_access[$value[module]]++;
									break;
								}			
							}
						}
						$_SESSION[modules_access]=$modules_access;
					}
					
					//redirects to the url entered before or to the main page (in no redirection url is specified)
					if(isset($_SESSION[redirect_url]))
					{
						$this->Redirect(substr($_SESSION[redirect_url],1));
					}
					else
					{
						$this->Redirect($check[default_page]);
					}
				}
			}
		}
				
		/**
	 	 * logs the user out, unsetting the $_SESSION variables
	 	 */
		function logout()
		{
			//saves dragndrop from session into database
			$dragndrop = serialize($_SESSION[dragndrop]);
			$this->GetModelObject('Users')->Update(array('id' => $_SESSION[userid], 'dragndrop' => $dragndrop), 'sp_users_update_dragndrop');
			
			$_SESSION = array();
			$this->Redirect('main/login');
		}
		
	}
?>